﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DelegatesAndEventsExercise
{
    class Program
    {
        #region Sequence
        public delegate void SequenceCounter();

        public static void DivpideByTwoSequencesum()
        {
            double prevSum = 0;
            double sum = 0;
            sum = 1;
            int index = 1;

            while (sum - prevSum >= 0.001)
            {
                prevSum = sum;
                sum += 1 / Math.Pow(2, index);
                index++;
            }

            Console.WriteLine(sum);
        }

        public static ulong Factorial(int num)
        {
            ulong product = 1;

            for (int i = 1; i <= num; ++i)
            {
                product *= (ulong)i;
            }


            return product;
        }

        public static void DivideByFactorial()
        {
            double prevSum = 0;
            double sum = 1;
            int index = 1;

            while (sum - prevSum >= 0.001)
            {
                prevSum = sum;

                sum += 1 / Factorial(index);
                index++;
            }

            Console.WriteLine(sum);
        }

        public static void DivideByTwoSequenceChangingSigns()
        {
            double prevSum = 0;
            double sum = 1;
            int index = 1;

            while (sum - prevSum >= 0.001)
            {
                prevSum = sum;
                sum += Math.Pow(-1, index - 1) * 1 / Math.Pow(2, index);
                index++;
            }

            Console.WriteLine(sum);
        }

        public void SequenceTest()
        {
            SequenceCounter del = new SequenceCounter(DivpideByTwoSequencesum);
            del += DivideByFactorial;
            del += DivideByTwoSequenceChangingSigns;

            del();
        }
        #endregion

        static void Main(string[] args)
        {
            PersonTest.Test();

        }
    }
}
